#!/bin/sh

# wrapper script to set up environment for butch-core
# and redirects to several extension scripts
# + aliases to some commands

[ "$STAGE" = "0" ] && [ -z "$CONFIG" ] && CONFIG="$PWD/config"
[ -z "$CONFIG" ] && CONFIG=/src/config

if [ ! -e "$CONFIG" ] ; then
	echo "error: $CONFIG not found"
	exit 1
fi

. "$CONFIG"

BINDIR=$(dirname "$(readlink -f "$0")")

# butch-core requires these paths
[ ! -d $C ] && mkdir -p $C
[ ! -d $LOGPATH ] && mkdir -p $LOGPATH

usage() {
cat << EOF >&2
butch v$(butch-core 2>&1 | head -n1 | sed -e 's@butch-core v@@')

usage: $0 command package ...

------- available commands -----
install [<package> ... ]  # build and install <packages>
download [<package> ... ] # download tarballs required by <packages>
rebuild [<package> ... ]  # rebuild already installed <packages>

install-service <args>    # install runit system service

relink <package>          # create symlinks for an unlinked <package>
unlink <package>          # remove symlinks to a specific <package>
rm [<package> ... ]       # completely remove installed <packages>
genfilelist <package>     # create filelist for <package>

pack [--method] <package> # create a binary archive of <package>
unpack <filename>         # install binary archive <filename> created with pack
builddeps <package>       # build and install dependencies of package

list                      # list installed packages
files <package>           # show files installed by <package>
mirrors <package>         # print mirrors for <package>
owner <file>              # print which package owns a <file>
users <package>           # print packages with <package> for dependency
deps [<package> ... ]     # print deps of <packages>

checksum <package>        # print checksums of files for a <package>
checkdownloads            # verify all package download links
checktarballs             # verify the downloaded package tarballs

printsec <pkg> <section>  # print the specified <section> of <package>
search <term>             # search for <term> in packages (grep syntax)
ucheck [<package> ... ]   # check for upstream updates in <packages>

dlinfo <url>              # download url, print initial butch recipe

update                    # rebuild installed packages with new recipes

download related actions are performed on-demand, i.e. the tarball is missing

EOF
}

prog=butch-core
case $1 in
	build) shift ; set -- install "$@";;
	download) shift ; set -- prefetch "$@";;
	pack) shift; prog=butch-pack;;
	unpack) shift; prog=butch-unpack;;
	genfilelist) shift; prog=butch-genfilelist;;
	builddeps) shift; prog=butch-builddeps;;
	list) shift; prog=butch-list;;
	owner) shift; prog=butch-owner;;
	files) shift; prog=butch-files;;
	search) shift; prog=butch-search;;
	ucheck) shift; prog="butch-ucheck --check";;
	printsec) shift; prog=butch-printsec;;
	mirrors) shift; prog=butch-mirrors;;
	users) shift; prog=butch-users;;
	deps) shift; prog=butch-deps;;
	checksum) shift; prog=butch-checksum;;
	checkdownloads) shift; prog=butch-checkdownloads;;
	checktarballs) shift; prog=butch-checktarballs;;
	dlinfo) shift; prog=butch-dlinfo;;
	install-service) shift; prog=butch-install-service;;
	unlink) shift ; prog=butch-unlink;; #remove links of pkg
	rm) shift; prog=butch-rm;;
	relink|relocate) shift; prog=butch-relocate;; #relinks the pkg into /
	update) export BUTCH_SKIPLIST=gcc3:stage0:stage0_musl:stage0_gcc:stage0_prepare:"$BUTCH_SKIPLIST" ;;
	install|prefetch|rebuild) : ;;
	*) usage && exit 1;;
esac

exec "$BINDIR"/$prog "$@"

